///<reference path="../../ag-grid-ext/cell-renders/render-operating-button.component.ts"/>
import { Component, Renderer2 } from '@angular/core';
import { GridConfigService } from '@core/config/grid-config.service';
import { AutopartsService } from '@core/services/autoparts/autoparts.service';
import { Events } from '@core/services/events/Events';
import { SubjectService } from '@core/services/events/subject.service';
import { ExpressionService } from '@core/services/expression/ExpressionService';
import { ApiService } from '@core/services/http/api.service';
import { EditorCheckBoxComponent } from '@shared/ag-grid-ext/cell-editors/edit-checkbox.component';
import { EditorChartSelectComponent } from '@shared/ag-grid-ext/cell-editors/editor-chart-select.component';
import { EditorCombogridComponent } from '@shared/ag-grid-ext/cell-editors/editor-combogrid.component';
import { EditorDateComponent } from '@shared/ag-grid-ext/cell-editors/editor-date.component';
import { EditorInputNumberComponent } from '@shared/ag-grid-ext/cell-editors/editor-input-number.component';
import { EditorInputComponent } from '@shared/ag-grid-ext/cell-editors/editor-input.component';
import { EditorSelectComponent } from '@shared/ag-grid-ext/cell-editors/editor-select.component';
import { EditorTextAreaComponent } from '@shared/ag-grid-ext/cell-editors/editor-textarea.component';
import { EditorTimeRangeComponent } from '@shared/ag-grid-ext/cell-editors/editor-time-range.component';
import { EditorUomSelectComponent } from '@shared/ag-grid-ext/cell-editors/editor-uom-select.component';
import { RenderButtonComponent } from '@shared/ag-grid-ext/cell-renders/render-button.component';
import { RenderDialogComponent } from '@shared/ag-grid-ext/cell-renders/render-dialog.component';
import { ApesRenderSelectComponent } from '@shared/ag-grid-ext/cell-renders/render-select.component';
import { ApesRenderTimeRangeComponent } from '@shared/ag-grid-ext/cell-renders/render-time-range.component';
import { ApesModalService } from '@shared/apes/modal';
import { Subscription } from 'rxjs';
import { AutopartsViewService } from '@core/services/autoparts/autoparts.view.service';
import { AutopartsApiService } from '@core/services/autoparts/autoparts.api.service';
import { RenderOperatingButtonComponent } from '@shared/ag-grid-ext/cell-renders/render-operating-button.component';
import { RenderSortComponent } from '@shared/ag-grid-ext/cell-renders/render-sort.component';
import { EditorTagSelectComponent } from '@shared/ag-grid-ext/cell-editors/editor-tag-select.component';
import { EditorInputGroupComponent } from '@shared/ag-grid-ext/cell-editors/editor-input-group.component';

@Component({
  templateUrl: './epc-parts-update.component.html',
  styleUrls: ['./epc-parts-update.component.scss'],
  selector: 'epc-parts-update',
})

export class EpcPartsUpdateComponent {
  domLayout: any;
  rowSelection: any;
  gridApi: any;
  gridColumnApi: any;
  localeText: any;
  context = {};
  frameworkComponents;  // 框架组件
  columnDefs: any[];   // 表头
  _rowData: any = [];
  /*级联*/
  valid = true;
  suppressRowClickSelection: boolean = false;
  queryData: any;
  data: any = {};


  saveDisabled: boolean = true;
  controlSaveSubscription: Subscription;
  changeParsePartListSubscription: Subscription;


  constructor(
    protected gridConfigService: GridConfigService,
    protected events: Events,
    protected expressionService: ExpressionService,
    private modalService: ApesModalService,
    protected apiService: ApiService,
    private renderer: Renderer2,
    private autopartsViewService: AutopartsViewService,
    private autopartsService: AutopartsService,
    private autopartsApiService: AutopartsApiService,
  ) {
    this.localeText = gridConfigService.getLocaleText();
    this.frameworkComponents = {
      input: EditorInputComponent,
      inputGroup:EditorInputGroupComponent,
      inputNumber: EditorInputNumberComponent,
      dialog: RenderDialogComponent,
      textarea: EditorTextAreaComponent,
      select: EditorSelectComponent,
      tagSelect: EditorTagSelectComponent,
      chartSelect: EditorChartSelectComponent,
      date: EditorDateComponent,
      comboGrid: EditorCombogridComponent,
      uomSelect: EditorUomSelectComponent,
      checkBox: EditorCheckBoxComponent,
      button: RenderButtonComponent,
      timeRange: EditorTimeRangeComponent,
      renderTimeRange: ApesRenderTimeRangeComponent,
      renderSelect: ApesRenderSelectComponent,
      dialogButton: RenderOperatingButtonComponent,
      sort: RenderSortComponent,
    };
    this.domLayout = 'autoHeight';
  }

  ngOnInit() {
    /**初始化表头**/
    this.rowSelection = 'single';
    this.domLayout = '';
    this.suppressRowClickSelection = false;
    this.columnDefs = [];
    this.setColumns();

    this.controlSaveSubscription = this.autopartsViewService.controlSaveBtn$.subscribe((data: boolean) => this.saveDisabled = data);

    this.changeParsePartListSubscription = this.autopartsViewService.parsePartListChange$.subscribe((data: any) => {
      if (data && data.data) {
        this._rowData = data.data;
        this.queryData = data;
        this.data = {};
        console.log('相关数据为：', data);
      } else {
        this._rowData = [];
        this.queryData = {};
        this.data = {};
      }
    });

  }

  ngOnDestroy() {
    this.controlSaveSubscription.unsubscribe();
    this.changeParsePartListSubscription.unsubscribe();
  }

  /**
   * 功能：重新设置表头
   * @param ctx
   */
  private setColumns() {
    this.columnDefs = [
      {
        headerName: '图号',
        field: 'svn',
        width: 90,
        editable: false,
      },
      {
        headerName: '排序图片',
        field: 'imgOrder',
        width: 120,
        editable: false,
      },
      {
        headerName: 'OEM码',
        field: 'partNumber',
        width: 90,
        editable: false,
      },
      {
        headerName: '图片',
        field: 'imgName',
        width: 120,
        editable: false,
      },
      {
        headerName: 'OEM描述',
        field: 'description',
        width: 120,
        editable: false,
      },
      {
        headerName: '配件名称（规则（编码:名称），多个用分号隔开）',
        field: 'partDescription',
        width: 120,
        editable: false,
      },
    ];

    if (this.gridApi) {
      this.gridApi.setColumnDefs(this.columnDefs);
      this.gridApi.sizeColumnsToFit();
    }
  }


  onGridReady(params) {
    this.gridApi = params.api;
    this.gridColumnApi = params.columnApi;

    params.api.sizeColumnsToFit();
  }

  /***
   * 保存数据到erp单据
   */
  saveData() {
    let saveData: any = {
      menuName: this.queryData.menuName, partChineseName: this.queryData.partChineseName,
      epcVersion: this.queryData.epcVersion, epcPartNumberParseList: this.queryData.data,
    };
    this.autopartsApiService.saveAsNativeForm(saveData).subscribe((data: any) => {
      this.data = data;
    });
  }
}
